package cn.tedu.cinema.employee.controller;

import cn.tedu.cinema.base.response.JsonResult;
import cn.tedu.cinema.employee.pojo.dto.EmployeeLoginParam;
import cn.tedu.cinema.employee.pojo.dto.EmployeeQuery;
import cn.tedu.cinema.employee.pojo.dto.EmployeeSaveParam;
import cn.tedu.cinema.employee.pojo.vo.EmployeeVO;
import cn.tedu.cinema.employee.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Slf4j
@RestController
@RequestMapping("/v1/employee")
public class EmployeeController {
    //Controller需要注入Service层,才可以调用业务层的方法!
    @Autowired
    EmployeeService employeeService;//注入的是接口!!!

    @PostMapping("login")
    public JsonResult login(@RequestBody EmployeeLoginParam employeeLoginParam){
        log.debug("用户登录:参数={}", employeeLoginParam);
        //调用业务层的login方法
        //Ctrl去接口  Ctrl+Alt去接口实现类  Ctrl+Shift去方法返回值类
        EmployeeVO employeeVO = employeeService.login(employeeLoginParam);
        //将业务层返回的employeeVO对象封装到JsonResult中返回给前端
        return JsonResult.ok(employeeVO);
    }

    @PostMapping("save")
    public JsonResult saveEmployee(EmployeeSaveParam employeeSaveParam){
        log.debug("保存用户:参数={}", employeeSaveParam);
        employeeService.saveEmployee(employeeSaveParam);
        return JsonResult.ok();
    }

    @GetMapping("select")
    public JsonResult selectEmployee(@RequestParam int pageNum,
                                     @RequestParam int pageSize,
                                     @ModelAttribute EmployeeQuery employeeQuery){
        log.debug("查询用户:参数={}", employeeQuery);

        return JsonResult.ok(employeeService.selectEmployee(pageNum, pageSize, employeeQuery));
    }

    @GetMapping("selectById")
    public JsonResult selectEmployeeById(Long id){
        log.debug("通过查询用户:参数={}", id);
        EmployeeVO list = employeeService.selectById(id);
        log.debug("查询到list::参数={}",list);
        return JsonResult.ok(list);
    }

    @PostMapping("/update/password/{employeeId}")
    public JsonResult resetPassword(@PathVariable Long employeeId){
        log.debug("重置密码:参数={}",employeeId);
        employeeService.resetPassword(employeeId);
        return JsonResult.ok();
    }

    @PostMapping("/update/status/{employeeId}/{status}")
    public JsonResult updateStatus(
            @PathVariable Long employeeId,@PathVariable String status){
        log.debug("修改用户状态:参数={},{}",employeeId,status);
        employeeService.updateStatus(employeeId,status);
        return JsonResult.ok();
    }

    @PostMapping("/delete/{employeeId}")
    public JsonResult deleteEmployee(@PathVariable Long employeeId){
        log.debug("删除用户:参数={}",employeeId);
        employeeService.deleteEmployee(employeeId);
        return JsonResult.ok();
    }
}
